package com.jlq.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jlq.dao.InvestorDao;
import com.jlq.pojo.Investor;
import com.jlq.pojo.Stock;
import com.jlq.service.InvestorService;
import com.jlq.vo.InvestorVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class InvestorServiceImpl implements InvestorService {

    @Autowired
    private InvestorDao investorDao;

    //分页查询
    public Map<String, Object> queryAllInvestor(Integer page, Integer limit,String field, String type, String name) {
        Page<Object> info = PageHelper.startPage(page, limit);
        List<InvestorVo> list = investorDao.getAllInvestor(field, type, name);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("data", list);
        map.put("code", 0);
        map.put("count", info.getTotal());
        return map;
    }

    //添加
    public String insert(InvestorVo investorVo, String code, String sname) {
        //添加股民表
        int num = investorDao.insertInvestor(investorVo);
        Stock stock = new Stock();
        stock.setCode(code);
        stock.setName(sname);
        Stock stock1 = investorDao.selectCodeAndName(stock);
        if(stock1 != null && stock1.getId() > 0){
            //表中有此股票，直接添加关系表
            int i1 = investorDao.insertRea(investorVo.getId(), stock1.getId());
            if(i1 > 0 && num > 0){
                return "ok";
            }else{
                return "no";
            }
        }else{
            //表中没有此股票，先添加到股票表，再添加中间表
            int i1 = investorDao.insertStock(stock);
            int i2 = investorDao.insertRea(investorVo.getId(), stock.getId());
            if(i1 >0 && i2 > 0 && num > 0){
                return "ok";
            }else{
                return "no";
            }
        }
    }

    //回显
    public Map<String, Object> updateInit(Integer id) {
        InvestorVo investor = investorDao.queryInvestorById(id);
        List<Stock> stocks = investorDao.queryStockById(id);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("investor", investor);
        map.put("stocks", stocks);
        return map;
    }

    //删除修改页面的已有股票
    public String deleteInvestorStock(Integer id, Integer stockId) {
        int i = investorDao.deleteInvestorStock(id, stockId);
        if(i > 0 ){
            return "ok";
        }else{
            return "no";
        }
    }

    //修改
    public String update(Investor investor,String code, String sname){
        //修改股民基本信息
        investorDao.updateInvestor(investor);
        if(code != "" && sname != null && code != null && sname != ""){
            Stock stock = new Stock();
            stock.setCode(code);
            stock.setName(sname);
            //查询此股票是否存在
            Stock stock1 = investorDao.selectCodeAndName(stock);
            if(stock1 != null && stock1.getId() > 0){
                //数据库中有此股票信息，直接添加
                investorDao.insertRea(investor.getId(), stock1.getId());
            }else{
                //当前股票没有，先添加到股票表，再添加关系表
                investorDao.insertStock(stock);
                investorDao.insertRea(investor.getId(), stock.getId());
            }
        }
        return "ok";
    }
}
